package info.jbohn.view;

import info.jbohn.data.FeedMessage;
import info.jbohn.model.RSSMainModel;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextArea;

public class RSSCenterUI extends JPanel implements ViewInterface
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JSplitPane splitPane;
	private JPanel panel;
	private JPanel rss_message_panel;
	private JScrollPane scrollPane;
	private JTable rss_messages_table;
	private String currentFeed = "";
	private String currentFeedMessage = "";
	DefaultTableModel tableModel;
	private JScrollPane scrollPane_1;
	private JTextArea textArea;
	public RSSCenterUI() {
		setLayout(new BorderLayout(0, 0));

		splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		this.add(splitPane);
		
		panel = new JPanel();
		splitPane.setRightComponent(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		scrollPane_1 = new JScrollPane();
		panel.add(scrollPane_1);
		
		textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		
		rss_message_panel = new JPanel();
		splitPane.setLeftComponent(rss_message_panel);
		rss_message_panel.setLayout(new BorderLayout(0, 0));
		
		scrollPane = new JScrollPane();
		rss_message_panel.add(scrollPane);
		
		rss_messages_table = new JTable();
		 tableModel = new DefaultTableModel();
		scrollPane.setViewportView(rss_messages_table);
	}

	@Override
	public void displayUI() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateUI(RSSMainModel model) 
	{
		
		List<FeedMessage> feedMessages = model.getFeedMessages(currentFeed);
		
		String[] columnNames = {"title"};
		String[][] data = new String[feedMessages.size()][1];
		
		for (int i = 0; i < feedMessages.size(); i++) {
			FeedMessage feedMessage = feedMessages.get(i);
			String titleMessage = feedMessage.getTitle();
			data[i][0] = titleMessage;
			if (currentFeedMessage.equalsIgnoreCase(titleMessage))
			{
				textArea.setText(feedMessage.getDescription());
			}
		}
		
		tableModel.setDataVector(data, columnNames);
		rss_messages_table.setModel(tableModel);
		
	}
	
	

	public String getCurrentFeed() {
		return currentFeed;
	}

	public String getCurrentFeedMessage() {
		return currentFeedMessage;
	}

	public void setCurrentFeedMessage(String currentFeedMessage) {
		this.currentFeedMessage = currentFeedMessage;
	}

	public void setCurrentFeed(String currentFeed) {
		this.currentFeed = currentFeed;
	}

	@Override
	public <T> void register(T list) {
		rss_messages_table.getSelectionModel().addListSelectionListener(
				(ListSelectionListener) list);
	}

}
